﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using BCData.Common.WebsiteConfiguration;
using BCDto.Common.WebsiteConfiguration;
using BCEntity.Common.WebsiteConfiguration;
using BCService.Utils;

namespace BCService.Common.WebsiteConfigurationService
{
    /// <summary>
    /// 网站配置服务
    /// </summary>
    public class WebsiteConfigurationService : IWebsiteConfigurationService
    {
        private readonly IWebsiteConfigurationData websiteConfigurationData;

        public WebsiteConfigurationService(IWebsiteConfigurationData websiteConfigurationData)
        {
            this.websiteConfigurationData = websiteConfigurationData;
        }

        public WebsiteConfigurationDto Get(int websiteConfigurationId)
        {
            return this.websiteConfigurationData.Get(websiteConfigurationId).As<WebsiteConfigurationDto>();
        }

        public WebsiteConfigurationDto Get()
        {
            return this.websiteConfigurationData.Get().As<WebsiteConfigurationDto>();
        }

        public IDictionary<string, string> GetWebsiteConfigurationTags()
        {
            IDictionary<string, string> dictionary = new Dictionary<string, string>();
            PropertyInfo[] properties = typeof(WebsiteConfigurationDto).GetProperties();

            foreach (PropertyInfo property in properties)
            {
                Attribute attribute = property.GetCustomAttribute(typeof(DisplayAttribute));
                DisplayAttribute displayAttribute = attribute as DisplayAttribute;
                dictionary.Add(property.Name, displayAttribute.Name);
            }
            return dictionary;
        }

        public WebsiteConfigurationDto Update(int websiteConfigurationId, WebsiteConfigurationPutDto websiteConfigurationPutDto)
        {
            WebsiteConfigurationEntity entity = this.websiteConfigurationData.Get(websiteConfigurationId);
            if (entity == null) {
                throw new ArgumentException("站点配置信息不存在。");
            }
            entity = websiteConfigurationPutDto.As<WebsiteConfigurationEntity>();
            entity.WebsiteConfigurationId = websiteConfigurationId;
            return this.websiteConfigurationData.Update(entity).As<WebsiteConfigurationDto>();
        }
    }
}
